﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.IO;
using System.Windows.Media.Imaging;
using JiveMessenger.ViewModels;
using GalaSoft.MvvmLight.Messaging;
using System.Diagnostics;

namespace JiveMessenger.Views
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class AddEmoticonWindow : Window
    {
        public string Shortcut { get; set; }
        public FileInfo SourcePath { get; set; }
        public byte[] ImageSource { get; set; }
        public AddEmoticonWindow(string imagePath = "", string shortCut = "")
        {
            InitializeComponent();
            RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
            ViewModelAddEmoticon vm = (ViewModelAddEmoticon)this.DataContext;
            if (!string.IsNullOrEmpty(imagePath))
                vm.EmoticonPath = imagePath;
            if (!string.IsNullOrEmpty(shortCut))
            {
                vm.OriginalShortCut = shortCut;
                vm.EmoticonShortcut = shortCut;
            }

            if (!string.IsNullOrEmpty(imagePath))
            {
                this.Title = "Edit Emoticon";
                this.TextBoxShortCut.Focus();
                vm.SetImage(imagePath);
            }

            Messenger.Default.Register<NotificationMessageAction<string>>(this, m =>
            {
                if (m.Notification == "BrowseForImage")
                {
                    m.Execute(BrowseForImage());
                }
            });

            Messenger.Default.Register<NotificationMessage>(this,m =>
                {
                    if (m.Notification == "FocusShortcutTextbox")
                    {
                        this.TextBoxShortCut.Focus();
                    }
                });
        }
   
        private string BrowseForImage()
        {
            var fd = new Microsoft.Win32.OpenFileDialog
                 {
                     Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG;)|*.BMP;*.JPG;*.GIF;*.PNG"
                 };

            if (!fd.ShowDialog().GetValueOrDefault())
                return null;

            return fd.FileName;
        }

        private void bnOk_Click(object sender, RoutedEventArgs e)
        {
            this.Shortcut = ((ViewModelAddEmoticon)this.DataContext).EmoticonShortcut;
            this.SourcePath = new FileInfo(((ViewModelAddEmoticon)this.DataContext).EmoticonPath);
            this.ImageSource = ((ViewModelAddEmoticon)this.DataContext).EmoticonImageSource;
            this.DialogResult = true;
        }

        private void bnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        protected override void OnClosed(EventArgs e)
        {
            ((ViewModelAddEmoticon)this.DataContext).Cleanup();
           Messenger.Default.Unregister(this);
            base.OnClosed(e);
        }
    }
}
